package com.eedi.framework.errorcode.dal.mapper;

import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.errorcode.controller.sys.vo.SysErrorCodePageReq;
import com.eedi.framework.errorcode.controller.sys.vo.SysErrorCodeResp;
import com.eedi.framework.mybatis.core.mapper.BaseMapperX;
import com.eedi.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.eedi.framework.errorcode.convert.SysErrorCodeConvert;
import com.eedi.framework.errorcode.dal.dataobject.SysErrorCodeDO;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

@Mapper
public interface ErrorCodeMapper extends BaseMapperX<SysErrorCodeDO> {

    default PageResult<SysErrorCodeDO> selectPage(SysErrorCodePageReq reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<SysErrorCodeDO>()
                .eqIfPresent(SysErrorCodeDO::getSysErrorCodeType, reqVO.getSysErrorCodeType())
                .likeIfPresent(SysErrorCodeDO::getSysErrorCodeAppName, reqVO.getSysErrorCodeAppName())
                .eqIfPresent(SysErrorCodeDO::getSysErrorCode, reqVO.getSysErrorCode())
                .likeIfPresent(SysErrorCodeDO::getSysErrorMessage, reqVO.getSysErrorMessage())
                .betweenIfPresent(SysErrorCodeDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(SysErrorCodeDO::getId));
    }

    default SysErrorCodeResp getByCode(Integer sysErrorCode){
        SysErrorCodeDO sysErrorCodeDO = selectOne(SysErrorCodeDO::getSysErrorCode, sysErrorCode);
        return SysErrorCodeConvert.INSTANCE.convert(sysErrorCodeDO);
    }

    default List<SysErrorCodeDO> selectListByCodes(Collection<Integer> codes) {
        return selectList(SysErrorCodeDO::getSysErrorCode, codes);
    }

    default SysErrorCodeDO selectByCode(Integer code) {
        return selectOne(SysErrorCodeDO::getSysErrorCode, code);
    }

    default List<SysErrorCodeDO> selectListByApplicationNameAndUpdateTimeGt(String applicationName, LocalDateTime minUpdateTime) {
        return selectList(new LambdaQueryWrapperX<SysErrorCodeDO>().eq(SysErrorCodeDO::getSysErrorCodeAppName, applicationName)
                .gtIfPresent(SysErrorCodeDO::getUpdateTime, minUpdateTime));
    }

}
